//Sequelize是一款基于Nodejs功能强大的异步ORM框架。

const Sequelize = require('sequelize');
const config = require('../config/config')
//连接数据库
const sequelize = new Sequelize(config.database, config.username, "", {
  host: config.host,
  dialect: config.dialect,

  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  },

  operatorsAliases: false
});

sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.');
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err);
  });


function getModelByName(name) {
  try {
    return sequelize.model(name)
  }
  catch (e) {
    return null;
  }
}



let User = sequelize.define('user', {
    id: {
      field: 'id',
      primaryKey: true,
      type: Sequelize.INTEGER(11).UNSIGNED,
      autoIncrement: true,
      allowNull: false
    },
    avatar: {
      field: 'avatar',
      type: Sequelize.STRING,
      allowNull: true
    },
    name: {
      field: 'name',
      type: Sequelize.STRING,
      allowNull: false
    },
    email: {
      field: 'email',
      type: Sequelize.STRING,
      allowNull: false
    },
    phone: {
      field: 'phone',
      type: Sequelize.STRING,
      allowNull: false
    },
    sex: {
      field: 'sex',
      type: Sequelize.INTEGER(1).UNSIGNED,
      allowNull: false
    },
    password: {
      field: 'password',
      type: Sequelize.STRING,
      allowNull: false
    },
    role_level: {
      field: 'role_level',
      type: Sequelize.INTEGER(11).UNSIGNED,
      allowNull: true
    },
    updated_at: {
      field: 'updated_at',
      type: Sequelize.INTEGER(11).UNSIGNED,
      allowNull: false
    },
    created_at: {
      field: 'created_at',
      type: Sequelize.INTEGER(11).UNSIGNED,
      allowNull: false
    }
  },
  {
    tableName: 'user',
    timestamps: false,
    freezeTableName: true
  }
);

let Session = sequelize.define('session', {
    id: {
      field: 'id',
      primaryKey: true,
      type: Sequelize.STRING(255),
      allowNull: false
    },
    userinfo: {
      field: 'userinfo',
      type: Sequelize.BLOB,
      allowNull: false
    },
    expired: {
      field: 'expired',
      type: Sequelize.INTEGER(11),
      allowNull: false
    }
  },
  {
    tableName: 'session',
    timestamps: false,
    freezeTableName: true
  }
);

let Article = sequelize.define('article', {
    id: {
      field: 'id',
      primaryKey: true,
      type: Sequelize.INTEGER(11).UNSIGNED,
      autoIncrement: true,
      allowNull: false
    },
    title: {
      field: 'title',
      type: Sequelize.STRING,
      allowNull: true
    },
    sub_title: {
      field: 'sub_title',
      type: Sequelize.STRING,
      allowNull: true
    },
    title_image: {
      field: 'title_image',
      type: Sequelize.STRING,
      allowNull: true
    },
    abstract: {
      field: 'abstract',
      type: Sequelize.STRING,
      allowNull: true
    },
    content: {
      field: 'content',
      type: Sequelize.BLOB,
      allowNull: false
    },
    page_view: {
      field: 'page_view',
      type: Sequelize.INTEGER(11).UNSIGNED,
      allowNull: false
    },
    user_id: {
      field: 'user_id',
      type: Sequelize.INTEGER(11).UNSIGNED,
      allowNull: false
    },
    updated_at: {
      field: 'updated_at',
      type: Sequelize.INTEGER(11).UNSIGNED,
      allowNull: false
    },
    created_at: {
      field: 'created_at',
      type: Sequelize.INTEGER(11).UNSIGNED,
      allowNull: false
    }
  },
  {
    tableName: 'article',
    timestamps: false,
    freezeTableName: true
  }
);

let Slide = sequelize.define('slide', {
    id: {
      field: 'id',
      primaryKey: true,
      type: Sequelize.INTEGER(11).UNSIGNED,
      autoIncrement: true,
      allowNull: false
    },
    image : {
      field: 'image',
      type: Sequelize.STRING,
      allowNull: true
    },
    title: {
      field: 'title',
      type: Sequelize.STRING,
      allowNull: true
    },
    link_url: {
      field: 'link_url',
      type: Sequelize.STRING,
      allowNull: true
    },
    remark: {
      field: 'remark',
      type: Sequelize.STRING,
      allowNull: true
    },
    weight: {
      field: 'weight',
      type: Sequelize.INTEGER(11).UNSIGNED,
      allowNull: false
    },
    is_show: {
      field: 'is_show',
      type: Sequelize.STRING(1),
      allowNull: false
    },
    user_id: {
      field: 'user_id',
      type: Sequelize.INTEGER(11).UNSIGNED,
      allowNull: false
    },
    updated_at: {
      field: 'updated_at',
      type: Sequelize.INTEGER(11).UNSIGNED,
      allowNull: false
    },
    created_at: {
      field: 'created_at',
      type: Sequelize.INTEGER(11).UNSIGNED,
      allowNull: false
    }
  },
  {
    tableName: 'slide',
    timestamps: false,
    freezeTableName: true
  }
);

let Company = sequelize.define('company', {
    id: {
      field: 'id',
      primaryKey: true,
      type: Sequelize.INTEGER(11).UNSIGNED,
      autoIncrement: true,
      allowNull: false
    },
    logo : {
      field: 'logo',
      type: Sequelize.STRING,
      allowNull: true
    },
    c_name: {
      field: 'c_name',
      type: Sequelize.STRING,
      allowNull: true
    },
    c_addr: {
      field: 'c_addr',
      type: Sequelize.STRING,
      allowNull: true
    },
    tel: {
      field: 'tel',
      type: Sequelize.STRING,
      allowNull: true
    },
    icp: {
      field: 'icp',
      type: Sequelize.STRING,
      allowNull: false
    },
    updated_at: {
      field: 'updated_at',
      type: Sequelize.INTEGER(11).UNSIGNED,
      allowNull: false
    },
    created_at: {
      field: 'created_at',
      type: Sequelize.INTEGER(11).UNSIGNED,
      allowNull: false
    }
  },
  {
    tableName: 'company',
    timestamps: false,
    freezeTableName: true
  }
);



/**
 * 判断电话和邮箱是否重复
 * @param phone
 * @param email
 * @returns {Promise<boolean>}
 */
async function isPhoneAndEMailRepeat(phone, email) {
  let isRepeat = false;

  await User.findOne({
    where: {phone: phone}
  }).then(user => {
    if (user && user.dataValues.phone != phone) {
      isRepeat = true
    }

  });

  await User.findOne({
    where: {email: email}
  }).then(user => {
    if (user && user.dataValues.email != email) {
      isRepeat = true
    }
  });

  return isRepeat;
}

module.exports={
  User,Session,Article,Slide,getModelByName,isPhoneAndEMailRepeat
};
